package com.yyds.service.impl;


import com.github.pagehelper.PageHelper;
import com.yyds.common.MyJSON;
import com.yyds.common.R;
import com.yyds.mapper.VirusVirusMapper;
import com.yyds.pojo.VirusVirus;
import com.yyds.service.VirusVirusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("virusVirusService")
@SuppressWarnings({"all"})//抑制警告
public class VirusVirusServiceImpl implements VirusVirusService {

    @Autowired
    VirusVirusMapper virusVirusMapper;

    @Override
    public MyJSON<VirusVirus> selectAllVirusVirusService(Map<String, Object> map) {
        Integer page = Integer.parseInt(map.get("page")+"");
        Integer limit = Integer.parseInt(map.get("limit")+"");
        System.out.println("通过分页查询病毒模块的病毒的全部信息 : page= "+page+" , limit= "+limit);
        int count = virusVirusMapper.selectAllVirusVirusMapper(map).size();
        //开启分页
        PageHelper.startPage(page,limit);
        List<VirusVirus> list = virusVirusMapper.selectAllVirusVirusMapper(map);

        MyJSON<VirusVirus> newsMyJSON = new MyJSON<>();
        newsMyJSON.setCode(0);
        newsMyJSON.setMsg("success");
        //数据库中总数居条数
        newsMyJSON.setCount((long)count);
        newsMyJSON.setData(list);
        return newsMyJSON;
    }

    @Override
    public R insertVirusVirusService(Map<String, Object> map) {
        return R.ok().message("添加了"+virusVirusMapper.insertVirusVirusMapper(map)+"条病毒信息");
    }

    @Override
    public R deleteVirusVirusService(Map<String, Object> map) {
        return R.ok().message("删除了"+virusVirusMapper.deleteVirusVirusMapper(map)+"条病毒信息");
    }

    @Override
    public R selectVirusVirusService(Map<String, Object> map) {
        return R.ok().data("virus",virusVirusMapper.selectVirusVirusMapper(map));
    }

    @Override
    public R deleteAllVirusVirusService(List<VirusVirus> list) {
        List<Integer> collect = list.stream()//得到一个流
                .map(VirusVirus::getVirus_id)//得到所有的id
                .collect(Collectors.toList());//将这些id放到一个 集合中
        //创建一个数组,并遍历存放id的集合,将所有的 数据放到 数组中
        int[] virus_ids = new int[collect.size()];
        Iterator<Integer> iterator = collect.iterator();
        for (int i = 0; iterator.hasNext(); i++) {
            virus_ids[i] = iterator.next();
        }
        int num = virusVirusMapper.deleteAllVirusVirusMapper(virus_ids);
        return R.ok().message("删除了"+num+"条新闻");
    }

    @Override
    public R updateVirusVirusService(Map<String, Object> map) {
        return R.ok().message("修改了"+virusVirusMapper.updateVirusVirusMapper(map)+"条信息");
    }

    @Override
    public R queryTypeVirusVirusService() {

        return R.ok().data("typePie",virusVirusMapper.queryTypeVirusVirusMapper());
    }

}
